package mo.tags.backtrack;

import mo.common.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class L113 {

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> combination = new ArrayList<>();
        dfs(res, combination, root, targetSum);
        return res;
    }

    private void dfs(List<List<Integer>> res, List<Integer> combination, TreeNode node, int target) {
        if (node == null) {
            return;
        }

        combination.add(node.val);
        target -= node.val;
        if (target == 0 && node.right == null && node.left == null) {
            res.add(new ArrayList<>(combination));
        }
        dfs(res, combination, node.left, target);
        dfs(res, combination, node.right, target);
        combination.remove(combination.size()-1);
    }

}
